package com.pet.shop.service.room;

import com.github.pagehelper.Page;
import com.pet.exceptions.BusinessException;
import com.pet.shop.dto.PageDto;
import com.pet.shop.entity.room.Hotel;
import com.pet.shop.entity.room.HotelTime;
import com.pet.shop.mappers.room.HotelMapper;
import com.pet.shop.mappers.room.HotelTimeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

@Service
public class HotelServiceImpl implements IHotelService {

    @Autowired
    private HotelMapper hotelMapper;
    @Autowired
    private HotelTimeMapper hotelTimeMapper;

    @Override
    public int deleteEntity(Long id) {
        return hotelMapper.delete(id);
    }

    @Override
    public int deleteEntityList(List<Long> idList) {
        if(idList == null || idList.isEmpty()) return 0;
        int i = 0;
        for (Long id : idList) {
            i += hotelMapper.delete(id);
        }
        return i;
    }

    @Override
    public Hotel insertEntity(Hotel record) {
        if(record == null || StringUtils.isEmpty(record.getName())){
            throw new BusinessException("房间信息不能为空");
        }
        if(record.getTimeList() == null || record.getTimeList().isEmpty()){
            throw new BusinessException("预约时间不能为空");
        }
        record.getTimeList().forEach(time -> {
            if(StringUtils.isEmpty(time.getTimeSlot())){
                throw new BusinessException("预约时间不能为空");
            }
        });
        hotelMapper.insert(record);
        record.getTimeList().forEach(time -> {
            time.setHotelId(record.getId());
            hotelTimeMapper.insert(time);
        });

        return record;
    }

    @Override
    public Hotel getEntity(Long id) {
        Hotel hotel = hotelMapper.get(id);
        if(hotel == null) return null;
        hotel.setTimeList(hotelTimeMapper.queryByHotelId(hotel.getId()));
        return hotel;
    }

    @Override
    public int updateEntity(Hotel record) {
        if(record.getTimeList() != null){
            Set<Long> timeIdList = hotelTimeMapper.queryIdByHotelId(record.getId());
            record.getTimeList().forEach(time -> {
                if(StringUtils.isEmpty(time.getTimeSlot())){
                    throw new BusinessException("预约时间不能为空");
                }
                if(time.getId() != null){
                    hotelTimeMapper.update(time);
                    timeIdList.remove(time.getId());
                }else{
                    time.setHotelId(record.getId());
                    hotelTimeMapper.insert(time);
                }
            });
            timeIdList.forEach(id -> hotelMapper.delete(id));
        }

        return hotelMapper.update(record);
    }

    @Override
    public PageDto<Hotel> queryPage(PageDto<Hotel> pageDto) {
        Page<Hotel> page = hotelMapper.queryPage(pageDto);
        pageDto.setPageResult(page);
        pageDto.getResults().forEach(hotel -> {
            hotel.setTimeList(hotelTimeMapper.queryByHotelId(hotel.getId()));
        });

        return pageDto;
    }

    @Override
    public List<HotelTime> queryHotelTime(Long hotelId, Date date) {
        return hotelTimeMapper.queryHotelTimeWithQuantity(hotelId, date);
    }
}
